﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Data.GameData;
using Data.GameData.Utils;

namespace GUIDialogs
{
    public partial class RaceDetails : BaseDialog
    {
        private Race race;

        public RaceDetails(bool newRace)
        {
            InitializeComponent();
            //Initialise();

            if( !newRace )
                this.race = Race.ActiveRace;

            InitialiseData(newRace);
        }
        public RaceDetails(Race race)
        {
            InitializeComponent();
            //Initialise();

            this.race = race;
            InitialiseData(false);
        }

        private void Initialise()
        {
            errorProvider.SetIconPadding(shortNameTextBox, 0);
            errorProvider.SetIconPadding(longNameTextBox, 0);
            errorProvider.SetIconPadding(hiTextBox, 0);
        }

        private void InitialiseData(bool newRace)
        {
            if (newRace)
            {
                this.Text = "New Race";

                this.hiTextBox.Text = UniqueRandom.Next(1, 10).ToString();
                this.colourButton.BackColor = Color.FromArgb(UniqueRandom.Next(50, 200), UniqueRandom.Next(50, 200), UniqueRandom.Next(50, 200));
            }
            else
            {
                this.shortNameTextBox.Text = race.Name;
                this.longNameTextBox.Text = "*";
                this.hiTextBox.Text = race.HabIndex.ToString();
                this.enviromentalComboBox.SelectedEnviroment = race.PerferedEnviroment;
                this.colourButton.BackColor = race.Colour;
            }
        }

        private void colourButton_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = this.colourButton.BackColor;
            DialogResult res = colorDialog1.ShowDialog();

            if (res == DialogResult.OK)
            {
                this.colourButton.BackColor = colorDialog1.Color;
            }            
        }

        public string RaceName
        {
            get { return shortNameTextBox.Text; }
            set { shortNameTextBox.Text = value; }
        }

        public string LongRaceName
        {
            get { return longNameTextBox.Text; }
            set { longNameTextBox.Text = value; }
        }

        public int HababilityIndex
        {
            get { return int.Parse(hiTextBox.Text); }
            set { hiTextBox.Text = value.ToString(); }
        }

        public SystemBodyEnviroment PerferedPlanetType
        {
            get { return enviromentalComboBox.SelectedEnviroment; }
            set { enviromentalComboBox.SelectedEnviroment = value; }
        }

        public Color RacialColour
        {
            get { return colourButton.BackColor; }
            set { colourButton.BackColor = value; }
        }

        protected override bool IsValid()
        {
            bool result = true;
            if (shortNameTextBox.Text.Length == 0)
            {
                errorProvider.SetError(shortNameTextBox, "You must enter a name");
                result = false;
            }
            else
            {
                errorProvider.SetError(shortNameTextBox, "");
            }

            return result;
        }

        protected override void UpdateData()
        {
            if (race != null)
            {
                race.Name = shortNameTextBox.Text;
                //this.longNameTextBox.Text = "*";
                race.HabIndex = int.Parse(hiTextBox.Text);
                race.PerferedEnviroment = enviromentalComboBox.SelectedEnviroment;
                race.Colour = colourButton.BackColor;
            }
        }
    }
}
